//jQuery time
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches
var isNoneInput = false;
initSelect();


$(".next").click(function(){
	if(animating) return false;
	animating = true;

	current_fs = $(this).parent();
	next_fs = $(this).parent().next();
	
	var exp = $(this).attr('data-value');
	if(exp != 'experiences') {
		current_fs.find('input').each(function(k,v){
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			var attname = $(this).attr('name');
			if(placeholder !== "" && typeof(placeholder) != 'undefined'){
				if(typeof(value) == 'undefined' || value == ""){
					isNoneInput = true;
					animating = false;
					layer.open({
						title:'系统提示',
						content:placeholder+'不可为空！'
					});
					return false;
				}
				if(attname == 'phone') {
					var phonerule = /^1[3|4|5|7|8]\d{9}$/;
					if(!phonerule.test(value)){
						isNoneInput = true;
						animating = false;
						layer.open({
							title:'系统提示',
							content:placeholder+'手机号格式不对！'
						});
						return false;
					}
				}
				if(attname == 'email'){
					var emailrule = /\w+[@]{1}\w+[.]\w+/;
					if(!emailrule.test(value)){
						isNoneInput = true;
						animating = false;
						layer.open({
							title:'系统提示',
							content:placeholder+'邮箱格式不对！'
						});
						return false;
					}
				}
			}
			isNoneInput = false;
		});
	}
	
	if(isNoneInput) return false; 
	
	//activate next step on progressbar using the index of next_fs
	$("#progressbar").eq($("fieldset").index(next_fs)).addClass("active");

	//show the next fieldset
	next_fs.show();
	//hide the current fieldset with style
	current_fs.animate({opacity: 0}, {
		step: function(now, mx) {
			//as the opacity of current_fs reduces to 0 - stored in "now"
			//1. scale current_fs down to 80%
			scale = 1 - (1 - now) * 0.2;
			//2. bring next_fs from the right(50%)
			left = (now * 50)+"%";
			//3. increase opacity of next_fs to 1 as it moves in
			opacity = 1 - now;
			current_fs.css({'transform': 'scale('+scale+')'});
			next_fs.css({'left': left, 'opacity': opacity});
		},
		duration: 800,
		complete: function(){
			current_fs.hide();
			animating = false;
		},
		//this comes from the custom easing plugin
		easing: 'easeInOutBack'
	});
});

$(".previous").click(function(){
	if(animating) return false;
	animating = true;

	current_fs = $(this).parent();
	previous_fs = $(this).parent().prev();

	//de-activate current step on progressbar
	$("#progressbar").eq($("fieldset").index(current_fs)).removeClass("active");

	//show the previous fieldset
	previous_fs.show();
	//hide the current fieldset with style
	current_fs.animate({opacity: 0}, {
		step: function(now, mx) {
			//as the opacity of current_fs reduces to 0 - stored in "now"
			//1. scale previous_fs from 80% to 100%
			scale = 0.8 + (1 - now) * 0.2;
			//2. take current_fs to the right(50%) - from 0%
			left = ((1-now) * 50)+"%";
			//3. increase opacity of previous_fs to 1 as it moves in
			opacity = 1 - now;
			current_fs.css({'left': left});
			previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
		},
		duration: 800,
		complete: function(){
			current_fs.hide();
			animating = false;
		},
		//this comes from the custom easing plugin
		easing: 'easeInOutBack'
	});
});
$(document).on('click','.now-resume-next',function(){
	if(animating) return false;
	animating = true;

	current_fs = $(this).parent().parent();
	next_fs = $('#needs_tab');

	if(isNoneInput) return false; 
	
	//activate next step on progressbar using the index of next_fs
	$("#progressbar").eq($("fieldset").index(next_fs)).addClass("active");

	//show the next fieldset
	next_fs.show();
	//hide the current fieldset with style
	current_fs.animate({opacity: 0}, {
		step: function(now, mx) {
			//as the opacity of current_fs reduces to 0 - stored in "now"
			//1. scale current_fs down to 80%
			scale = 1 - (1 - now) * 0.2;
			//2. bring next_fs from the right(50%)
			left = (now * 50)+"%";
			//3. increase opacity of next_fs to 1 as it moves in
			opacity = 1 - now;
			current_fs.css({'transform': 'scale('+scale+')'});
			next_fs.css({'left': left, 'opacity': opacity});
		},
		duration: 800,
		complete: function(){
			current_fs.hide();
			animating = false;
		},
		//this comes from the custom easing plugin
		easing: 'easeInOutBack'
	});
});

/**
 * 地区 根据父分类获取子分类
 */
function childAreas(pid,uri,id){
	$.ajax({
		'url':'/'+uri,
		'type':'GET',
		'data':'pid='+pid,
		error: function(request) {
			$('#'+id).html('<option value="0">没有更多分类</option>');
		},
		success: function(data){
			if(data.status == 'success'){
				var result = data.data;
				var options ="";
				var firstid;
				if(result.length > 0){
					$.each(result,function(k,e){
						if(k==0) firstid=e.area_id;
						options +='<option value="'+e.area_id+'">'+e.title+'</option>';
					});
					if(id == 'city'){
						childAreas(firstid,uri,'area');
					}
					$('#'+id).html(options);
				}else{
					$('#'+id).html('<option value="0">没有更多分类</option>');
				}
				$('#'+id).selectpicker('refresh');
			}else{
				$('#'+id).html('<option value="0">没有更多分类</option>');
			}
		},
	});
}

function delDiv(idName){
	$('.del-div').click(function(){
		var id = $(this).attr('data-id');
		$("#"+idName+'_'+id).remove();
	});
}
function date(){
	$('.form_datetime').datetimepicker({
		format:'yyyy-mm',
		language:  'zh-CN',
		weekStart: 1,
		todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 4,
		minView: 3,
		forceParse: 0,
		endDate:new Date(),
	}).on('changeDate', function(ev){
		var newdate = $(this).val();
		var arr = newdate.split('-');
		var smounth = parseInt(arr[1])-2;
		var sDate = arr[0]+"-"+smounth+'-01';
		if($(this).next().hasClass('form_datetime')){
			$(this).next().datetimepicker('setStartDate', sDate);
		}
		if($(this).prev().hasClass('form_datetime')){
			$(this).prev().datetimepicker('setEndDate', newdate);
		}
	});
}

$('.next-talk').click(function(){
	tipsDialog('你确定？','','确认拒绝','稍加考虑','');
});
$('.publish-last').click(function(){
	window.location.href="/member/need";
});
$('.confirm-publish').click(function(){
	tipsDialog('据说参加能力测评，职业性格测试','企业更喜欢追哦','依旧发布','返回参加','/member/need/create');
});
$('.nexttest').click(function(){
	tipsDialog('你确定？','','算了吧','难不倒我','');
});
$('.nexttestlast').click(function(){
	tipsDialog('只差一步喽','','算了吧','come on','');
});
$(".finish").click(function(){
	var data = $('#msform').serializeArray();
	var params = {
			url: '/member/resume/save',
			type: 'POST',
			dataType: 'json',
			data: data,
	};
  $.ajax(params).done(function(resp){
	  if(resp.status == 'success') {
		  $('#resume_select').append('<option value="'+resp.data.id+'">'+resp.data.name+'</option>');
		  $('#resume_select').selectpicker('refresh');
	  } else {
		  layer.msg('网络异常...');
	  }
  }).fail(function(erro){
	  error(erro);
  });
});
$('#needs_tab ').on('click','.nltest',function(){
	var url = $(this).attr('data-uri');
	var uri = '/member/need/create';
	var params = {
			url: uri,
			type: 'POST',
			dataType: 'json',
			data: $('#msform').serializeArray(),
	};
	$.ajax(params).done(function(resp){
		if(resp.status == 'success') {
			layer.msg('请稍后，正在跳转。。。');
			$('#msform input[name=need_id]').val(resp.data.id);
			url = url+"?nid="+resp.data.id+"&next=1";
			window.location.href=url;
		} else {
			layer.open({
				title:'提示',content:resp.msg
			});
		}
	}).fail(function(erro){
		error(erro);
	});
});

function tipsDialog(msg1,msg2,btn0,btn1,uri){
	$(".msg-one").html(msg1);
	$(".msg-two").html(msg2);
	layer.open({
		type: 1,
		title:false,
		btn: [btn0, btn1],
		closeBtn: 0,
		shift: 2,
		shadeClose: true, //开启遮罩关闭
		area: ['320px', '200px'], //宽高
		content: $('#tips-dialog'),
		btn1: function(index, layero){  //不同按钮回调
			if(uri=='' || typeof(uri) == 'undefined'){
				window.location.href="/member/resume";
			} else {
				var params = {
						url: uri,
						type: 'POST',
						dataType: 'json',
						data: $('#msform').serializeArray(),
				};
				$.ajax(params).done(function(resp){
					if(resp.status == 'success') {
						$('#msform input[name=need_id]').val(resp.data.id);
						window.location.href="/member/need";
					} else {
						layer.open({
							title:'提示',content:resp.msg
						});
					}
				}).fail(function(erro){
					error(erro);
				});
			}
		},
		btn2: function(index, layero){
			
		}
	});
}
